package com.yidu.hbatsams.action.AtsData;
import com.yidu.hbatsams.utils.AtsDataShowTree;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

 /*
 * @Author 谭俊
 * @Description //TODO 导出工具类
 * @Date 16:19 2019/12/24 0024
 * @Param 
 * @return 
 **/
public class ExcelTest {

    public HSSFWorkbook  sds(List<Map<String,Object>> lis, HttpServletResponse response){

        HSSFWorkbook wb = new HSSFWorkbook();
        try {

            HSSFSheet sheet = wb.createSheet("new sheet");
            HSSFCellStyle style = wb.createCellStyle(); // 样式对象
            sheet.setDefaultColumnWidth(20);
              List<AtsDataShowTree> asd = (List<AtsDataShowTree>)lis.get(0).get("data");
            HSSFRow row= sheet.createRow((short)0);
        sdss(asd,row,1,"");

            for (int i=0;i<lis.size();i++){
                HSSFRow rows = sheet.createRow((short) i+1);
                HSSFCell cell = rows.createCell((short) 0);
                cell.setCellValue((String)(lis.get(i).get("names")));
                List<AtsDataShowTree> cao=(   List<AtsDataShowTree>)(lis.get(i).get("data"));
                for (int j=0;j<cao.size();j++){
                    sdsss(cao,rows,1);

                }
            }
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            String ste=sdf.format(new Date());
//            final String userAgent = request.getHeader("user-agent");
//            if (userAgent != null && userAgent.indexOf("Firefox") >= 0) {
//                fileName = new String(fileName.getBytes(), "ISO8859-1");
//            } else {
//                fileName = URLEncoder.encode(fileName, "UTF8");
//            }
            // 下载文件
            response.setContentType("application/octet-stream");
            response.setHeader("Content-disposition", "attachment;filename="+ste+".xls");
            response.flushBuffer();
            wb.write(response.getOutputStream());
            wb.close();
            System.out.println("okdsadsad");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return wb;
    }
    public int sdss( List<AtsDataShowTree>  lis, HSSFRow row ,int call,String name){


        for (int i=0;i<lis.size();i++){
            if (lis.get(i).getChildren().size()>0){
                name+=lis.get(i).getName()+"\r\n";
                call= sdss(lis.get(i).getChildren(),row,call,name);
                name="";

            }else{
                HSSFCell cell1= row.createCell(call);
                cell1.getCellStyle().setWrapText(true);
                cell1.getCellStyle().setAlignment(HorizontalAlignment.CENTER);
                cell1.setCellValue(name+lis.get(i).getName()+"\r\n("+lis.get(i).getLabel()+")");
                call++;
            }

        }
        return call;
    }

    public int sdsss( List<AtsDataShowTree>  lis, HSSFRow row ,int call){


        for (int i=0;i<lis.size();i++){
            if (lis.get(i).getChildren().size()>0){

                call= sdsss(lis.get(i).getChildren(),row,call);


            }else{
                HSSFCell cell1= row.createCell(call);
                cell1.getCellStyle().setWrapText(true);

                cell1.setCellValue(lis.get(i).getValue());
                call++;
            }

        }
        return call;
    }
}
